package com.hhwy.controller;

import com.hhwy.HslCommunication.Core.Types.OperateResult;
import com.hhwy.HslCommunication.Profinet.Siemens.SiemensPLCS;
import com.hhwy.HslCommunication.Profinet.Siemens.SiemensS7Net;
import com.hhwy.controller.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/ccys/main/control")
@Slf4j
public class CableConveyorsController {

    @Value("${cable-conveyors-socket.ip}")
    private String ip;

    /**
     * 向前
     * @return
     */
    @GetMapping("/forward")
    public Result forward(){
        try{
            SiemensS7Net siemens_net = new SiemensS7Net(SiemensPLCS.S1200,ip);
            OperateResult connect = siemens_net.ConnectServer();
            if(connect.IsSuccess){
                log.info("/forward connect success!");
            }
            else {
                log.error("failed:"+connect.Message);
                return new Result(0,connect.Message);
            }
            siemens_net.ConnectClose();
            siemens_net.Write("M10.1",true);
            return new Result(1,"调用成功");
        }catch (Exception e){
            return new Result(0,"调用失败:"+e.getMessage());
        }
    }

    /**
     * 向后
     * @return
     */
    @GetMapping("/backoff")
    public Result backoff(){
        try{
            SiemensS7Net siemens_net = new SiemensS7Net(SiemensPLCS.S1200,ip);
            OperateResult connect = siemens_net.ConnectServer();
            if(connect.IsSuccess){
                log.info("/backoff connect success!");
            }
            else {
                log.error("failed:"+connect.Message);
                return new Result(0,connect.Message);
            }
            siemens_net.ConnectClose();
            siemens_net.Write("M10.1",false);
            return new Result(1,"调用成功");
        }catch (Exception e){
            return new Result(0,"调用失败:"+e.getMessage());
        }
    }

    /**
     * 加速度
     * @return
     */
    @GetMapping("/addSpeed")
    public Result addSpeed(){
        try{
            SiemensS7Net siemens_net = new SiemensS7Net(SiemensPLCS.S1200,ip);
            OperateResult connect = siemens_net.ConnectServer();
            if(connect.IsSuccess){
                log.info("/backoff connect success!");
            }
            else {
                log.error("failed:"+connect.Message);
                return new Result(0,connect.Message);
            }
            siemens_net.ConnectClose();
            siemens_net.Write("M11.0",true);
            siemens_net.Write("M11.0",false);
            return new Result(1,"调用成功");
        }catch (Exception e){
            return new Result(0,"调用失败:"+e.getMessage());
        }
    }

    /**
     * 减速度
     * @return
     */
    @GetMapping("/deceSpeed")
    public Result deceSpeed(){
        try{
            SiemensS7Net siemens_net = new SiemensS7Net(SiemensPLCS.S1200,ip);
            OperateResult connect = siemens_net.ConnectServer();
            if(connect.IsSuccess){
                log.info("/backoff connect success!");
            }
            else {
                log.error("failed:"+connect.Message);
                return new Result(0,connect.Message);
            }
            siemens_net.ConnectClose();
            siemens_net.Write("M11.1",true);
            siemens_net.Write("M11.1",false);
            return new Result(1,"调用成功");
        }catch (Exception e){
            return new Result(0,"调用失败:"+e.getMessage());
        }
    }

    /**
     * 停止
     * @return
     */
    @GetMapping("/stop")
    public Result stop(){
        try{
            SiemensS7Net siemens_net = new SiemensS7Net(SiemensPLCS.S1200,ip);
            OperateResult connect = siemens_net.ConnectServer();
            if(connect.IsSuccess){
                log.info("/backoff connect success!");
            }
            else {
                log.error("failed:"+connect.Message);
                return new Result(0,connect.Message);
            }
            siemens_net.ConnectClose();
            siemens_net.Write("M12.0",true);
            siemens_net.Write("M12.0",false);
            return new Result(1,"调用成功");
        }catch (Exception e){
            return new Result(0,"调用失败:"+e.getMessage());
        }
    }



}
